package com.act.util;

import java.io.IOException;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 读取配置文件内容
 */
public final class SystemConfig {
	
	private static final Log log = LogFactory.getLog(SystemConfig.class);
	
	private static final String CONF_FILE = "/config.properties";
	
	private static final Properties config = new Properties();

	static {
		try {
			config.load(SystemConfig.class.getResourceAsStream(CONF_FILE));
		} catch (IOException e) {
			if (log.isErrorEnabled()) {
				log.error(e.getMessage(), e);
			}
			throw new RuntimeException("加载配置文件" + CONF_FILE + "出错", e);
		}
	}

	private SystemConfig() {
		
	}

	/**
	 * 获取key对应的value
	 * @param key
	 * @return
	 */
	public static String getProperty(String key) {
		return StringUtils.trim(config.getProperty(key));
	}

	/**
	 * 设置键值
	 * @param key
	 * @param value
	 */
	public static void setProperty(String key, String value) {
		config.setProperty(key, value);
	}

	/**
	 * 获取键值
	 * 
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static String getProperty(String key, String defaultValue) {
		String value = StringUtils.trim(config.getProperty(key));
		if (StringUtils.isNotBlank(value)) {
			value = defaultValue;
		}
		return value;
	}
	
	public static int getInteger(String key){
		int value = 0;
		try{
			String config = getProperty(key);
			value = Integer.parseInt(config);
		}catch(Exception e){
			if(log.isDebugEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return value;
	}
}